class UsersController < ApplicationController
  #render json: @user.errors, status: :unprocessable_entity

  #**
  # @apiDefine ReturnUser
  #
  # @apiSuccess {String} username 用户名
  # @apiSuccess {String} name 姓名
  # @apiSuccess {Number} age 年龄
  #*

  #**
  # @apiDefine ReturnUserArray
  #
  # @apiSuccess {User[]} _ 用户列表
  # @apiSuccess {String} _.username 用户名
  # @apiSuccess {String} _.name 姓名
  # @apiSuccess {Number} _.age 年龄
  #**

  #**
  # @apiDefine UserParams
  #
  # @apiParam {String} username 用户名
  # @apiParam {String} name 姓名
  # @apiParam {Number} age 年龄
  #*

  #**
  # @api {get} /users 列出所有用户
  # @apiName GetUsers
  # @apiGroup User
  #
  # @apiUse ReturnUserArray
  #*
  def index
    render json: User.all
  end

  #**
  # @api {post} /users 创建用户
  # @apiName PostUser
  # @apiGroup User
  #
  # @apiUse UserParams
  # @apiUse ReturnUser
  #*
  def create
    user = User.create!(user_param)
    render json: user
  end

  #**
  # @api {get} /users/:id 查看用户
  # @apiName GetUser
  # @apiGroup User
  #
  # @apiUse ReturnUser
  #*
  def show
    user = User.find(params[:id])
    render json: user
  end

  #**
  # @api {put} /users/:id 更新用户
  # @apiName PutUser
  # @apiGroup User
  #
  # @apiUse UserParams
  # @apiUse ReturnUser
  #*
  def update
    user = User.find(params[:id])
    user.update!(user_param)
    render json: user
  end

  private

  def user_param
    params.permit(:username, :name, :age)
  end
end
